package com.yunhe.tomcat.student;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yunhe.tomcat.iservice.IStudentTurnDao;
import com.yunhe.tomcat.iservice.impl.StudentTurnImpl;
import com.yunhe.tomcat.model.Student;
import net.sf.json.JSONArray;

/**
 * Servlet implementation class IndexServlet
 */
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public IndexServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 设置请求以及响应的内容类型以及编码方式
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");

        // 1.实例化接口实现类
        IStudentTurnDao stu = new StudentTurnImpl();
        // 2.返回查询结果
        List<Student> list = stu.listStident();

        // 3.将list放进map里
        List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();
        if (list != null && !list.isEmpty() && list.size() > 0) {
            for (Student s : list) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("studentId", s.getStudentId());
                map.put("studentName", s.getStudentName());
                map.put("studentSex", s.getStudentSex());
                map.put("studentAge", s.getStudentAge());
                 map.put("studentDate", s.getStudentDate());
                map.put("studentDateStr", s.getStudentDateStr());
                mList.add(map);
            }
        }

        // 4.将mList对象放进map对象
        /*
         * Map<String, Object> jsonMap = new HashMap<String, Object>();
         * jsonMap.put("data", mList);
         */
        // 5.将list对象转换成json对象
        JSONArray json = JSONArray.fromObject(mList);
        // 6.将json对象转换成字符串
        String str = json.toString();
        // 7.向客户端发送字符串
        PrintWriter out = response.getWriter();
        out.write(str); // 将str发送到前台

        // 8.转到index.html
        // request.getRequestDispatcher("index.html").forward(request, response);

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub

        doGet(request, response);

    }

}
